from django.db import models
from game.encounter.models import encounter

import random

# Create your models here.

class map(models.Model):
    
    name = models.CharField(max_length = 20, unique = True)
    hight = models.IntegerField(default = 0)
    width = models.IntegerField(default = 0)
    #Image to use for no location
    baseNode = models.CharField(max_length = 50, default="default.jpg")
    
    def get_absolute_url(self):
        return "/map/"+str(self.id)
    
class location(models.Model):
    
    name = models.CharField(max_length = 20, unique = True)
    map = models.ForeignKey('map', default = 0)
    xCord = models.IntegerField(default = 0)
    yCord = models.IntegerField(default = 0)
    activeImage = models.CharField(max_length = 50, default = "defaultLoc.jpg")
    inactiveImage = models.CharField(max_length = 50, default = "defaultInLoc.jpg")
    #linkToMap is used if the location leads to a different map
    linkToMap = models.ForeignKey('map', blank = True, null = True, related_name="linkedLocation")
    #url is used if the location leads somewhere else, ie a shop
    url = models.CharField(max_length = 50, blank = True, null = True)
    encounters = models.ManyToManyField(encounter, blank = True, null = True)    
    
    def get_absolute_url(self):
        #if self.linkToMap is not None:
        #    mapLink = self.linkToMap
        #    return mapLink.get_absolute_url()
        if self.url is not None:
            return self.url
        elif self.encounters:
            return "/location/"+str(self.name)
        else:
            return None
    def chooseEncounter(self):
        encList = []
        #Add the encounter to the array for each probability. Will select one from the array
        for enc in self.encounters.all():
            for x in xrange(enc.probability):
                encList.append(enc)
        encounter = encList[random.randint(0, len(encList)-1)]
        return encounter
    def isActive(self):
        return True